﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

using Gurucore.Framework.Core.Activation;
using Gurucore.Framework.Test;

namespace Gurucore.Framework.Test
{
	public class TestInspector
	{
		private Assembly m_oAssembly;

		public TestInspector(string p_sAssembly)
		{
			DynamicActivator oDynActivator = Gurucore.Framework.Core.Application.GetInstance().GetGlobalSharedObject<DynamicActivator>();
			m_oAssembly = oDynActivator.GetAssembly(p_sAssembly);
		}

		public Assembly GetAssembly()
		{
			return m_oAssembly;
		}

		public List<Type> GetTestClasses()
		{
			List<Type> arrTestClasses = new List<Type>();
			Type[] arrTypes = m_oAssembly.GetTypes();
			foreach (Type oType in arrTypes)
			{
				if (oType.IsSubclassOf(typeof(TestSuite)))
				{
					arrTestClasses.Add(oType);
				}
			}
			return arrTestClasses;
		}

		public List<MethodInfo> GetTestMethods(Type p_oTestClass)
		{
			MethodInfo[] arrMethods = p_oTestClass.GetMethods();
			List<MethodInfo> arrTestMethods = new List<MethodInfo>();
			foreach (MethodInfo oMethod in arrMethods)
			{
				object[] arrAttr = oMethod.GetCustomAttributes(typeof(TestCaseAttribute), false);
				if (arrAttr.Length > 0)
				{
					arrTestMethods.Add(oMethod);
				}
			}
			return arrTestMethods;
		}
	}
}
